package com.dandelion.api.manager;

import com.dandelion.api.request.UserAddressRequest;
import com.dandelion.api.service.UserAddressService;
import com.dandelion.common.dao.auto.pojo.UserAddress;
import com.dandelion.common.domain.UserAddressVO;
import com.rockit.core.pojo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressManager {
    @Autowired
    private UserAddressService userAddressService;

    public List<UserAddressVO> getUserAddressList(long userId, Pagination pagination) {
        return userAddressService.getUserAddressList(userId, pagination);
    }

    public void addNewUserAddress(long userId, UserAddressRequest request) {
        UserAddress address = toUserAddress(request);
        userAddressService.addNewUserAddress(userId, address);
    }

    public void deleteUserAddress(Long userId, Long addressId) {
        userAddressService.deleteUserAddress(userId, addressId);
    }

    public void updateUserAddress(Long userId, Long addressId, UserAddressRequest request) {
        UserAddress address = toUserAddress(request);
        userAddressService.updateUserAddress(userId, addressId, address);
    }

    private static UserAddress toUserAddress(UserAddressRequest request) {
        UserAddress address = new UserAddress();
        address.setProvCode(request.getProvCode());
        address.setCityCode(request.getCityCode());
        address.setAreaCode(request.getAreaCode());
        address.setDistrict(request.getDistrict());
        address.setMobile(request.getMobile());
        address.setName(request.getName());
        address.setIsDefault(request.getIsDefault());
        return address;
    }
}
